package com.tao.carousel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tao.carousel.mapper.CarouselMapper;
import com.tao.carousel.service.CarouselService;
import com.tao.pojo.Carousel;
import com.tao.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 86130
 */
@Slf4j
@Service
public class CarouselServiceImpl implements CarouselService {

    @Autowired
    private CarouselMapper carouselMapper;

    /**
     * 查询优先级最高的6条轮播图数据
     * 按照优先级查找数据库数据
     *  使用stream流，进行内存数据切割，保存6条数据
     * @return
     */
    @Cacheable(value = "list.carousel",key = "#root.methodName",cacheManager = "cacheManagerDay")
    @Override
    public R list() {
        QueryWrapper<Carousel> carouselQueryWrapper = new QueryWrapper<>();
        carouselQueryWrapper.orderByDesc("priority");
        List<Carousel> list = carouselMapper.selectList(carouselQueryWrapper);

        //jdk 1.8stream
        List<Carousel> collect = list.stream().limit(6).collect(Collectors.toList());

        R ok = R.ok(collect);
        log.info("CarouselServiceImpl.list业务结束，结果：{}", ok);
        return ok;
    }
}
